package com.leetcode.题库.score1;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @BelongsProject: LeetCode
 * @BelongsPackage: com.leetcode.题库.score1
 * @Author: lei
 * @CreateTime: 2024-03-12  22:31
 * @Description: 给定一个字符串s，最多只能进行一次变换，返回变换后能得到的最小字符串(按照字典序进行比较)。变化规则:交换字符串中任意两个不同位置字符。
 * 输入描述: 一串小写字母组成的字符串s
 * 输出描述: 按照要求进行变换得到最小字符串
 * 备注: s是都是小写字符组成1<=s.length<=1000
 * 示例1:
 * 输入：abcdef
 * 输出：abcdef
 * 示例2:
 * 输入：bcdefa
 * 输出：acdefb
 * 说明：a和b进行位置交换，可以得到最小字符串
 * @Version: 1.0
 */
public class 字符串变换最小字符串 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            char[] chars = s.toCharArray();
            char[] newChars = Arrays.copyOf(chars, chars.length);
            Arrays.sort(newChars);
            for (int i = 0; i < chars.length; i++) {
                // chars    =   bcdefa
                // newChars =   abcdef
                if (chars[i] != newChars[i]) {
                    for (int j = chars.length - 1; j >= 0; j--) {
                        if (newChars[i] == chars[j]) {
                            char aChar = chars[i];
                            chars[i] = chars[j];
                            chars[j] = aChar;
                            break;
                        }
                    }
                    break;
                }
            }
            System.out.println(Arrays.toString(chars));

        }
    }

}
